package com.samphanie.auiu.biz_home.fragment.home

import android.os.Bundle
import android.view.View
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.samphanie.auiu.common.ui.component.AuiuAbsListFragment
import com.samphanie.auiu.library.log.AuiuLog
import com.samphanie.auiu.library.restful.annotation.CacheStrategy
import com.samphanie.auiu.biz_home.model.HomeModel
import com.samphanie.auiu.ui.item.AuiuDataItem

class HomeTabFragment : AuiuAbsListFragment() {

    private lateinit var viewModel: HomeViewModel

    private var cateId: String? = null
    val DEFAULT_HOT_TAB_CATEGORY_ID = "0"

    companion object {
        fun newInstance(cateId: String): HomeTabFragment {
            val args = Bundle()
            args.putString("cateId", cateId)
            val fragment = HomeTabFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        cateId = arguments?.getString("cateId", DEFAULT_HOT_TAB_CATEGORY_ID)
        super.onViewCreated(view, savedInstanceState)

        viewModel = ViewModelProvider(this)[HomeViewModel::class.java]

        queryTabCategoryList(CacheStrategy.CACHE_FIRST)
        enableLoadMore { queryTabCategoryList(CacheStrategy.NET_ONLY) }
    }

    override fun onRefresh() {
        super.onRefresh()
        queryTabCategoryList(CacheStrategy.NET_CACHE)
    }

    override fun createLayoutManager(): RecyclerView.LayoutManager {
        val isHotTab = cateId.equals(DEFAULT_HOT_TAB_CATEGORY_ID)
        return if (isHotTab) super.createLayoutManager() else GridLayoutManager(context, 2)
    }

    private fun queryTabCategoryList(cacheStrategy: Int) {
        AuiuLog.e("选中的分类ID$cateId")
        viewModel.queryTabCategoryList(cateId!!, pageNum, 10, cacheStrategy).observe(viewLifecycleOwner) {
            if (it != null) {
                updateUI(it)
            } else {
                finishRefresh(null)
            }
        }
        //        homeApiService().queryTabCategoryList(cacheStrategy, cateId!!, pageNum, 10)
        //            .enqueue(object : AuiuCallback<HomeModel> {
        //                override fun onSuccess(response: AuiuResponse<HomeModel>) {
        //                    if (response.successful() && response.data != null) {
        //                        updateUI(response.data!!)
        //                    } else {
        //                        finishRefresh(null)
        //                    }
        //                }
        //
        //                override fun onFailed(throwable: Throwable) {
        //                    finishRefresh(null)
        //                }
        //
        //            })
    }

    private fun updateUI(data: HomeModel) {
        if (!isAlive) return
        val dataItems = mutableListOf<AuiuDataItem<*, *>>()
        val bannerList = data.bannerList
        if (!bannerList.isNullOrEmpty()) {
            dataItems.add(BannerItem(data.bannerList))
        }
//        data.bannerList?.let {
//            dataItems.add(BannerItem(data.bannerList))
//        }

        val subCategoryList = data.subCategoryList
        if (!subCategoryList.isNullOrEmpty()) {
            dataItems.add(GridItem(data.subCategoryList))
        }
//        data.subCategoryList?.let {
//            dataItems.add(GridItem(data.subCategoryList))
//        }

        val goodsList = data.goodsList
        if (!goodsList.isNullOrEmpty()) {
            goodsList.forEachIndexed { index, goodsModel ->
                dataItems.add(GoodsItem(goodsModel, cateId.equals(DEFAULT_HOT_TAB_CATEGORY_ID)))
            }
        }
//        data.goodsList?.forEachIndexed { _, goodsModel ->
//            dataItems.add(GoodsItem(goodsModel, cateId.equals(DEFAULT_HOT_TAB_CATEGORY_ID)))
//        }

        finishRefresh(dataItems)
    }

}